//
//  VisitorView.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/12.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate:NSObjectProtocol{

    func loginBtnDidClick()
    func registerBtnDidClick()

}


class VisitorView: UIView {
    
    weak var visiterViewDelegate:VisitorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
        circleViewAddAnimation()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //给circleView添加动画
    func circleViewAddAnimation(){
    
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAnimation.repeatCount = MAXFLOAT
        
        baseAnimation.toValue = 2 * M_PI
        
        baseAnimation.duration = 10
        
        
        baseAnimation.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(baseAnimation, forKey: nil)
    
    }
    
    
    
    //添加控件
    @objc private func setUpUI(){
        
        addSubview(circleImageView)
        circleImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        addSubview(backView)
        backView.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        addSubview(largeImageView)
        largeImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        addSubview(tipLabel)
        tipLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        addSubview(loginBtn)
        loginBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        addSubview(registerBtn)
        registerBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        
        
        
        addConstraint(NSLayoutConstraint(item: largeImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: largeImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        
        addConstraint(NSLayoutConstraint(item: circleImageView , attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: largeImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: largeImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: largeImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //loginBtn与registerBtn
        addConstraint(NSLayoutConstraint(item: loginBtn , attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
        
        addConstraint(NSLayoutConstraint(item: registerBtn , attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -20))
        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: tipLabel.bounds.width - 200))
        
        
        //backView
        
//     addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(<#format: String#>, options: <#NSLayoutFormatOptions#>, metrics: <#[NSObject : AnyObject]?#>, views: <#[NSObject : AnyObject]#>))
        
        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Right , relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    
    func loginBtnClick(){
    
    visiterViewDelegate?.loginBtnDidClick()
    
    }
    
    func registerBtnClick(){
    
    visiterViewDelegate?.registerBtnDidClick()
    
    }
    
    
    
    lazy var largeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var circleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var tipLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", text_Color: UIColor.grayColor(), fontSize: 14, maxWidth: 300)
    
    
    
    lazy var loginBtn:UIButton = UIButton(title: "登录", imageName: "common_button_white_disable")
  
    
    lazy var registerBtn:UIButton = UIButton(title: "注册", imageName: "common_button_white_disable")
      
}
